/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fbv.tcc.xls;

import fbv.tcc.classes.PontoRadiacao;
import fbv.tcc.gui.Programa;
import fbv.tcc.utils.PropertiesGenerator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
 *
 * @author Casa
 */
public class XlsReader {

    /*
     * preciso carregar o arquivo; OK
     * jogar os dados num hashMap (dia e radiacao); ok
     * concatenar os dados com mais de uma planilha;ok
     * ordenar essa lista;ok
     * gerar os graficos
     */
    private ArrayList<PontoRadiacao> pointRadiationList;
    private String yearDayHour = "";
    private double radiation;

    //GETTERS
    public ArrayList<PontoRadiacao> getPointRadiationList() {
        return pointRadiationList;
    }

    public String getYearDayHour() {
        return yearDayHour;
    }

    public double getRadiation() {
        return radiation;
    }

    //SETTERS
    public void setPointRadiationList(ArrayList<PontoRadiacao> pointRadiationList) {
        this.pointRadiationList = pointRadiationList;
    }

    public void setYearDayHour(String yearDayHour) {
        this.yearDayHour = yearDayHour;
    }

    public void setRadiation(double radiation) {
        this.radiation = radiation;
    }

    //private String filename = "G:\\Victor\\Dropbox\\FBV\\TCC\\Pasta sem titulo\\Radiationxx.xls";
    /*
     * CONSTRUCTORS
     */
    public XlsReader() {
    }

    /*
     * METHODS
     */
    /*
     *carrega a planilha, e retorna os dados num ArrayList.
     */
    public ArrayList<PontoRadiacao> loadSheetFile(String file) throws BiffException, FileNotFoundException, IOException, ParseException {

        Properties res = PropertiesGenerator.carregarProperties();


        //Cria a lista onde serão armazenados os pontos
        pointRadiationList = new ArrayList<PontoRadiacao>();

        //Carrega o arquivo
        Workbook workbook = Workbook.getWorkbook(new File(file));

        //Carrega a planilha de fato
        Sheet[] sheets = workbook.getSheets();

        int colunaData = Integer.parseInt(res.getProperty("colunaData"));
        int colunaRadiacao = Integer.parseInt(res.getProperty("colunaRadiacao"));

        for (int i = 0; i < sheets.length; i++) {
            for (int j = 4; j < sheets[i].getRows(); j++) {

                Cell timeStamp = sheets[i].getCell(colunaData, j);
                Cell radiationCell = sheets[i].getCell(colunaRadiacao, j);

                yearDayHour = timeStamp.getContents();
                radiation = Double.parseDouble(radiationCell.getContents().replaceAll(",", "."));

                pointRadiationList.add(new PontoRadiacao(yearDayHour, radiation));

            }
        }
        return pointRadiationList;
    }
}
